package com.example.user.controller;

import com.example.user.constant.CookieConstant;
import com.example.user.constant.RedisConstant;
import com.example.user.dataObject.UserInfo;
import com.example.user.enums.ResultEnum;
import com.example.user.enums.Roleenum;
import com.example.user.service.UserService;
import com.example.user.util.CookieUtil;
import com.example.user.util.ReslutVoUtil;
import com.example.user.vo.ReslutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 买家登录
     *
     * @param openid
     * @param response
     * @return
     */
    @RequestMapping("/buyer")
    public ReslutVo buyer(@RequestParam("openid") String openid, HttpServletResponse response) {
        //和数据库进行匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ReslutVoUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //判断角色
        if(Roleenum.BUYER.getCode()!=userInfo.getRole()){
            return ReslutVoUtil.error(ResultEnum.ROLE_ERROR);
        }

        //cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID,openid,CookieConstant.expier);
        return ReslutVoUtil.sucess();
    }

    /**
     * 卖家登录
     *
     * @param openid
     * @param response
     * @return
     */
    @RequestMapping("/seller")
    public ReslutVo seller(@RequestParam("openid") String openid, HttpServletResponse response, HttpServletRequest request) {
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie!=null&&!StringUtils.isEmpty( stringRedisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_TEMPLATE,cookie.getValue())))){
            return ReslutVoUtil.logged();
        }

        //和数据库进行匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ReslutVoUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //判断角色
        if(Roleenum.SLLER.getCode()!=userInfo.getRole()){
            return ReslutVoUtil.error(ResultEnum.ROLE_ERROR);
        }

        //cookie里设置token=UUID, redis设置key=UUID, value=xyz
        String token = UUID.randomUUID().toString();
        String key = String.format(RedisConstant.TOKEN_TEMPLATE,token);
        Integer expier=CookieConstant.expier;
        stringRedisTemplate.opsForValue().set(key,openid,expier,TimeUnit.SECONDS );

        //cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID,openid,CookieConstant.expier);
        CookieUtil.set(response, CookieConstant.TOKEN,token,CookieConstant.expier);
        return ReslutVoUtil.sucess();
    }

}
